package com.gxa.customer.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxa.common.commons.R;
import com.gxa.common.params.PrAddParam;
import com.gxa.common.params.QueryPrParam;
import com.gxa.common.pojo.PrInfo;
import com.gxa.common.utils.RUtils;
import com.gxa.customer.mapper.PrMapper;
import com.gxa.customer.service.PrService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class PrServiceImpl implements PrService {
    @Autowired
    private PrMapper prMapper;

    @Override
    public R AddPr(PrAddParam param) {
        if (Objects.nonNull(param)) {
            PrInfo prInfo = new PrInfo();
            BeanUtils.copyProperties(param, prInfo);
            prMapper.insert(prInfo);
            return RUtils.success();
        }
        return RUtils.fail();
    }

    @Override
    public R updatePr(PrAddParam param) {
        if (Objects.nonNull(param)) {
            PrInfo prInfo = new PrInfo();
            BeanUtils.copyProperties(param, prInfo);
            prMapper.updateById(prInfo);
            return RUtils.success();
        }
        return RUtils.fail();
    }

    @Override
    public R delPr(Long id) {
        if(Objects.nonNull(id)){
            prMapper.deleteById(id);
            return RUtils.success();
        }
        return RUtils.fail();
    }

    @Override
    public R queryPr(QueryPrParam param) {
        QueryWrapper<PrInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("nation_id",param.getNationId()).eq("customer_id",param.getCustomerId());
        List<PrInfo> list = prMapper.selectList(wrapper);
        return RUtils.success(list);
    }
}
